package com.realbrand.net.realbrandpay.ui.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.realbrand.net.realbrandpay.R;
import com.realbrand.net.realbrandpay.api.HTTPAPI;
import com.realbrand.net.realbrandpay.base.BaseActivity;
import com.realbrand.net.realbrandpay.bean.VersionBean;
import com.realbrand.net.realbrandpay.db.DBManger;
import com.realbrand.net.realbrandpay.inter.IVersion;
import com.realbrand.net.realbrandpay.untils.Constant;
import com.realbrand.net.realbrandpay.untils.HttpRequest;
import com.realbrand.net.realbrandpay.untils.Static;
import com.realbrand.net.realbrandpay.zxing.app.CaptureActivity;
import com.realbrand.net.realbrandpay.zxing.util.L;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/11/23.
 */

public class OneActivity extends BaseActivity {
    public static final String VALUE_ADS = "adsActivity";
    public static boolean aBoolean = true;
    SharedPreferences sharedPreferences;
    //是否需要获取本地版本信息
    public static boolean isGetVersion = true;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        new DBManger(this).deleteSQL();
        sharedPreferences = getSharedPreferences(Constant.shareName, MODE_PRIVATE);
        String account = sharedPreferences.getString("account", "");
        Intent intent = new Intent(OneActivity.this, CaptureActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(Static.BundleKey, VALUE_ADS);
        intent.putExtras(bundle);
        startActivity(intent);
        finish();
        getVersion();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.one_layout;
    }
    @Override
    protected void onRestart() {
        super.onRestart();
        Intent intent = new Intent(OneActivity.this, CaptureActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(Static.BundleKey, VALUE_ADS);
        intent.putExtras(bundle);
        startActivity(intent);
        finish();
    }
    private void getVersion() {
        IVersion iVersion = HttpRequest.request_BASE_URL_VERSION(IVersion.class, HTTPAPI.BASE_URL_VERSION);
        iVersion.getVersion()
                .subscribeOn(Schedulers.newThread())//请求在新线程中执行
                .observeOn(Schedulers.io())//请求完成后再IO中执行
                .doOnNext(new Action1<VersionBean>() {
                    @Override
                    public void call(VersionBean versionBean) {
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())//最后再主线程中执行
                .subscribe(new Subscriber<VersionBean>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(VersionBean versionBean) {
                        L.d(L.TAG, "版本信息" + versionBean.getVersionCode());
                        SharedPreferences.Editor edit = sharedPreferences.edit();
                        edit.putInt(Constant.VersionUpData, versionBean.getVersionCode());
                        edit.putString(Constant.VersionUpDataUrl, versionBean.getUrl());
                        L.d(L.TAG, "下载地址" + versionBean.getUrl());
                        if (isGetVersion) {
                            int appLocalVersion = Static.getAPPLocalVersion(OneActivity.this);
                            //保存本地版本号
                            edit.putInt(Constant.VersionBenDi, appLocalVersion);
                            edit.putInt(Constant.VersionCompare, appLocalVersion);
                            edit.commit();
                        }

                    }
                });


    }
    @Override
    protected void setOnClick() {

    }

    @Override
    public void onClick(View v) {

    }
}
